package com.woniu.controller;

import com.woniu.entity.Result;
import com.woniu.pojo.ClassChange;
import com.woniu.service.IClassChangeService;
import com.woniu.vo.cardVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaohuang
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/classChange")
@Api(tags = "课时费管理")
public class ClassChangeController extends BaseController{
    @Autowired
    private IClassChangeService classChangeService;

    @ApiOperation("添加")
    @PostMapping
    public Result add(@RequestBody ClassChange classChange){
        boolean flag = classChangeService.saveOrUpdate(classChange);
        return toResult(flag);
    }

    @ApiOperation("添加会员卡课时费")
    @PostMapping("/cardClassChange")
    public Result saveCardClassChange(@RequestBody cardVo[] cardVo){
        if (cardVo.length == 0){
            return null;
        }
        ClassChange classChange = new ClassChange();
        Collection<ClassChange> classChanges = new LinkedList<>();

            for (cardVo vo : cardVo) {
                BeanUtils.copyProperties(vo,classChange);
                classChanges.add(classChange);
            }

        boolean flag = classChangeService.saveOrUpdateBatch(classChanges);
        return toResult(flag);
    }



    @ApiOperation("通过id查询")
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id){
        ClassChange classChange = classChangeService.getById(id);
        return toDataResult(classChange);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result modifyById(@RequestBody ClassChange classChange){
        boolean flag = classChangeService.updateById(classChange);
        return toResult(flag);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        boolean flag = classChangeService.removeById(id);
        return toResult(flag);
    }

    @ApiOperation(("根据课表id和会员卡id查询"))
    @GetMapping("/app/{timetableId}/{cardId}")
    public Result byTimetableIdAndCardId(@PathVariable String timetableId,
                                         @PathVariable String cardId){
        ClassChange classChange = classChangeService.byTimetableIdAndCardId(timetableId,cardId);
        return toDataResult(classChange);
    }

}
